package com.atguigu.gmall.sms.service.impl;

import com.atguigu.gmall.sms.dao.SkuFullReductionDao;
import com.atguigu.gmall.sms.dao.SkuLadderDao;
import com.atguigu.gmall.sms.entity.SkuFullReductionEntity;
import com.atguigu.gmall.sms.entity.SkuLadderEntity;
import com.atguigu.gmall.sms.vo.ItemSkuSaleVO;
import com.atguigu.gmall.sms.vo.SkuSaleVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.core.bean.PageVo;
import com.atguigu.core.bean.Query;
import com.atguigu.core.bean.QueryCondition;

import com.atguigu.gmall.sms.dao.SkuBoundsDao;
import com.atguigu.gmall.sms.entity.SkuBoundsEntity;
import com.atguigu.gmall.sms.service.SkuBoundsService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuBoundsService")
public class SkuBoundsServiceImpl extends ServiceImpl<SkuBoundsDao, SkuBoundsEntity> implements SkuBoundsService {
    @Autowired
    private SkuLadderDao skuLadderDao;
    @Autowired
    private SkuFullReductionDao skuFullReductionDao;
    @Override
    public PageVo queryPage(QueryCondition params) {
        IPage<SkuBoundsEntity> page = this.page(
                new Query<SkuBoundsEntity>().getPage(params),
                new QueryWrapper<SkuBoundsEntity>()
        );

        return new PageVo(page);
    }

    /**
     * 保存三张表
     * @param skuSaleVO
     */
    @Override
    @Transactional
    public void saveSkuSaleInfo(SkuSaleVO skuSaleVO) {
        //3.1、保存sms_sku_bound
        SkuBoundsEntity skuBoundsEntity=new SkuBoundsEntity();
        skuBoundsEntity.setSkuId(skuSaleVO.getSkuId());
        skuBoundsEntity.setBuyBounds(skuSaleVO.getBuyBounds());
        skuBoundsEntity.setGrowBounds((skuSaleVO.getGrowBounds()));

        List<Integer> work = skuSaleVO.getWork();
        skuBoundsEntity.setWork(work.get(3)*1+work.get(2)*2+work.get(1)*4+work.get(0)*8);
        this.save(skuBoundsEntity);
        //3.2、保存sms_sku_ladder
        SkuLadderEntity skuLadderEntity=new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuSaleVO.getSkuId());
        skuLadderEntity.setFullCount(skuSaleVO.getFullCount());
        skuLadderEntity.setDiscount(skuSaleVO.getDiscount());
        skuLadderEntity.setAddOther(skuSaleVO.getLadderAddOther());
        skuLadderDao.insert(skuLadderEntity);
        //3.3、保存sms_sku_full_resucation
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        skuFullReductionEntity.setSkuId(skuSaleVO.getSkuId());
        skuFullReductionEntity.setFullPrice(skuSaleVO.getFullPrice());
        skuFullReductionEntity.setAddOther(skuSaleVO.getLadderAddOther());
        skuFullReductionEntity.setReducePrice(skuSaleVO.getReducePrice());
        skuFullReductionDao.insert(skuFullReductionEntity);
    }


    @Override
    public List<ItemSkuSaleVO> querySkuSaleBySkuId(Long skuId) {
        List<ItemSkuSaleVO> saleVOS=new ArrayList<>();
        //查询积分信息
        SkuBoundsEntity skuBoundsEntity = this.getOne(new QueryWrapper<SkuBoundsEntity>().eq("sku_id", skuId));
        if(skuBoundsEntity!=null){
            ItemSkuSaleVO itemSkuSaleVO=new ItemSkuSaleVO();
            itemSkuSaleVO.setType("积分");
            StringBuffer sb=new StringBuffer();
            if(skuBoundsEntity.getGrowBounds()!=null&&skuBoundsEntity.getGrowBounds().intValue()>0){
                sb.append("成长积分送"+skuBoundsEntity.getGrowBounds());
            }
            if(skuBoundsEntity.getBuyBounds()!=null && skuBoundsEntity.getBuyBounds().intValue()>0){
                if(StringUtils.isNotBlank(sb)){
                    sb.append(", ");
                }
                sb.append(",赠送积分送"+skuBoundsEntity.getBuyBounds());
            }
            itemSkuSaleVO.setDesc(sb.toString());
            saleVOS.add(itemSkuSaleVO);

        }

        //查询打折
        SkuLadderEntity skuLadderEntity = this.skuLadderDao.selectOne(new QueryWrapper<SkuLadderEntity>().eq("sku_id", skuId));
        if(skuBoundsEntity!=null){
            ItemSkuSaleVO ladderVo=new ItemSkuSaleVO();
            ladderVo.setType("打折");
            StringBuffer sb=new StringBuffer();
            if(skuLadderEntity.getFullCount()>0){
                sb.append("满"+skuLadderEntity.getFullCount()+"件");
            }
            if(skuLadderEntity.getDiscount()!=null && skuLadderEntity.getDiscount().intValue()>0){
                if(StringUtils.isNotBlank(sb)){
                    sb.append(", ");
                }
                sb.append("打"+skuLadderEntity.getDiscount().divide(new BigDecimal(10))+"折");
            }
            ladderVo.setDesc(sb.toString());
            saleVOS.add(ladderVo);

        }

        //查询满减
        SkuFullReductionEntity reductionEntity = this.skuFullReductionDao.selectOne(new QueryWrapper<SkuFullReductionEntity>().eq("sku_id", skuId));
        if(reductionEntity!=null){

            ItemSkuSaleVO reductionVo=new ItemSkuSaleVO();
            reductionVo.setType("满减");
            StringBuffer sb=new StringBuffer();
            if(reductionEntity.getFullPrice()!=null && reductionEntity.getFullPrice().intValue()>0){
                sb.append("满"+reductionEntity.getFullPrice()+"钱");
            }
            if(reductionEntity.getReducePrice()!=null && reductionEntity.getReducePrice().intValue()>0){
                sb.append("减"+reductionEntity.getReducePrice()+"钱");
            }
            reductionVo.setDesc(sb.toString());
            saleVOS.add(reductionVo);
        }
        return saleVOS;
    }

}